package com.expdatacloud.service.impl;

import com.expdatacloud.entity.YtWorkOrderDetail;
import com.expdatacloud.mapper.YtWorkOrderDetailMapper;
import com.expdatacloud.service.YtWorkOrderDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;

import javax.annotation.Resource;
import java.util.List;
/**
 * 工单表明细(YtWorkOrderDetail)表服务实现类
 *
 * @author makejava
 * @since 2020-09-29 14:00:27
 */
@Service("ytWorkOrderDetailService")
public class YtWorkOrderDetailServiceImpl implements YtWorkOrderDetailService {
    @Autowired
    DataSourceTransactionManager dataSourceTransactionManager;

    @Autowired
    TransactionDefinition transactionDefinition;

    @Autowired
    private YtWorkOrderDetailMapper ytWorkOrderDetailDao;

    /**
     * 通过ID查询单条数据
     *
     * @param detailid 主键
     * @return 实例对象
     */
    @Override
    public YtWorkOrderDetail queryById(Object detailid) {
        return this.ytWorkOrderDetailDao.queryById(detailid);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<YtWorkOrderDetail> queryAllByLimit(int offset, int limit) {
        return this.ytWorkOrderDetailDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param ytWorkOrderDetail 实例对象
     * @return 实例对象
     */
    @Override
    public YtWorkOrderDetail insert(YtWorkOrderDetail ytWorkOrderDetail) {
        this.ytWorkOrderDetailDao.insert(ytWorkOrderDetail);
        return ytWorkOrderDetail;
    }

    @Override
    public int insertList(List<YtWorkOrderDetail> ytWorkOrderDetaillist) {
        //TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);
        int count = 0;
        try {
            count = ytWorkOrderDetailDao.insertList(ytWorkOrderDetaillist);
            //dataSourceTransactionManager.commit(transactionStatus);     //手动提交
        }catch (Exception e){
            //dataSourceTransactionManager.rollback(transactionStatus);       //事务回滚
            throw new RuntimeException(e);
        }

        return count;
    }

    /**
     * 修改数据
     *
     * @param ytWorkOrderDetail 实例对象
     * @return 实例对象
     */
    @Override
    public YtWorkOrderDetail update(YtWorkOrderDetail ytWorkOrderDetail) {
        this.ytWorkOrderDetailDao.update(ytWorkOrderDetail);
        return this.queryById(ytWorkOrderDetail.getDetailid());
    }

    /**
     * 通过主键删除数据
     *
     * @param detailid 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Object detailid) {
        return this.ytWorkOrderDetailDao.deleteById(detailid) > 0;
    }
}